package com.example.sqlitelogin;

import android.graphics.Point;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
//import android.support.v7.AppCompatActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.sqlitelogin.service.UserService;
import com.example.sqlitelogin.tools.CustomActionBar;
import com.example.sqlitelogin.tools.SelfPasswordTransformationMethod;


public class RegisterActivity extends AppCompatActivity {
    EditText username;
    EditText password,qrpassword;
    EditText age;
    RadioGroup sex;
    Button register;
    private ToggleButton tbControl1,tbControl2;//掩码显隐开关
    private SelfPasswordTransformationMethod transformationMethod1,transformationMethod2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();
        findViews();

        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String name = username.getText().toString().trim();
                String pass = password.getText().toString().trim();
                String pass2 = qrpassword.getText().toString().trim();
                String agestr = age.getText().toString().trim();
                String sexstr = ((RadioButton) RegisterActivity.this.findViewById(sex.getCheckedRadioButtonId())).getText().toString();

                if (agestr.equals("")|name.equals("")|pass.equals("") |pass2.equals("")){
                    Toast toast=Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT);
                    toast.show();
                }
                else
                {   if (pass.equals(pass2)){
                        Log.i("TAG", name + "_" + pass + "_" + agestr + "_" + sexstr);
                        UserService uService = new UserService(RegisterActivity.this);
                        User user = new User();
                        user.setUsername(name);
                        user.setPassword(pass);
                        user.setAge(Integer.parseInt(agestr));
                        user.setSex(sexstr);
                        uService.register(user);

                        Toast toast=Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT);
                        toast.show();
                    }else{
                    Toast toast=Toast.makeText(RegisterActivity.this, "账号密码不一致", Toast.LENGTH_SHORT);
                    toast.show();
                }
                }

            }
        });
    }

    private void findViews() {
        username = (EditText) findViewById(R.id.usernameRegister);
        password = (EditText) findViewById(R.id.passwordRegister);
        qrpassword= (EditText) findViewById(R.id.qrusernameRegister);
        age = (EditText) findViewById(R.id.ageRegister);
        sex = (RadioGroup) findViewById(R.id.sexRegister);
        register = (Button) findViewById(R.id.Register);
        tbControl1 = findViewById(R.id.tbControl);
        tbControl2 = findViewById(R.id.tbControl2);

        transformationMethod1 = new SelfPasswordTransformationMethod('*');//可根据自己喜欢设置
        transformationMethod2 = new SelfPasswordTransformationMethod('*');//可根据自己喜欢设置

        password.setTransformationMethod(transformationMethod1);
        qrpassword.setTransformationMethod(transformationMethod2);
        tbControl1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //不用调用实例：.getInstance() （否则会变回默认掩码）
                    password.setTransformationMethod(transformationMethod1);
                }
            }
        });
        tbControl2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    qrpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //不用调用实例：.getInstance() （否则会变回默认掩码）
                    qrpassword.setTransformationMethod(transformationMethod2);
                }
            }
        });


    }


    private void init() {
        CustomActionBar actionBar0 = findViewById(R.id.actionbar_0);


    }

}






